﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using System.Data.SqlClient;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class Article_DAL:IArticle
    {
        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddArticle(Article_Model model)
        {
            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[26];
            
            //设置参数的值
            paras[0].dbType = SqlDbType.Text;
            paras[0].objValue = model.Title;
            paras[0].parName = "@Title";

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].objValue = model.Author;
            paras[1].parName = "@Author";

            paras[2].dbType = SqlDbType.VarChar;
            paras[2].objValue = model.CopyFrom;
            paras[2].parName = "@CopyFrom";

            paras[3].dbType = SqlDbType.Int;
            paras[3].objValue = model.IsMaster;
            paras[3].parName = "@IsMaster";

            paras[4].dbType = SqlDbType.Int;
            paras[4].objValue = model.EditorId;
            paras[4].parName = "@EditorId";

            paras[5].dbType = SqlDbType.VarChar;
            paras[5].objValue = model.KeyWord;
            paras[5].parName = "@KeyWord";

            paras[6].dbType = SqlDbType.Int;
            paras[6].objValue = model.Hits;
            paras[6].parName = "@Hits";


            paras[7].dbType = SqlDbType.DateTime;
            paras[7].objValue = model.UpdateTime;
            paras[7].parName = "@UpdateTime";

            paras[8].dbType = SqlDbType.Int;
            paras[8].objValue = model.IsHot;
            paras[8].parName = "@IsHot";

            paras[9].dbType = SqlDbType.Int;
            paras[9].objValue = model.IsTop;
            paras[9].parName = "@IsTop";

            paras[10].dbType = SqlDbType.Int;
            paras[10].objValue = model.IsCommend;
            paras[10].parName = "@IsCommend";

            paras[11].dbType = SqlDbType.Int;
            paras[11].objValue = model.IsPassed;
            paras[11].parName = "@IsPassed";

            paras[12].dbType = SqlDbType.Text;
            paras[12].objValue = model.Content;
            paras[12].parName = "@Content";

            paras[13].dbType = SqlDbType.Int;
            paras[13].objValue = model.IncludePic;
            paras[13].parName = "@IncludePic";

            paras[14].dbType = SqlDbType.VarChar;
            paras[14].objValue = model.DefaultPicUrl;
            paras[14].parName = "@DefaultPicUrl";

            paras[15].dbType = SqlDbType.Int;
            paras[15].objValue = model.ReadScore;
            paras[15].parName = "@ReadScore";

            paras[16].dbType = SqlDbType.Int;
            paras[16].objValue = model.ClassID;
            paras[16].parName = "@ClassID";

            paras[17].dbType = SqlDbType.Int;
            paras[17].objValue = model.IsDel;
            paras[17].parName = "@IsDel";

            paras[18].dbType = SqlDbType.VarChar;
            paras[18].objValue = model.TitleFontColor;
            paras[18].parName = "@TitleFontColor";

            paras[19].dbType = SqlDbType.Int;
            paras[19].objValue = model.TitleFontType;
            paras[19].parName = "@TitleFontType";

            paras[20].dbType = SqlDbType.Int;
            paras[20].objValue = model.TitleFontBlod;
            paras[20].parName = "@TitleFontBlod";

            paras[21].dbType = SqlDbType.Int;
            paras[21].objValue = model.TitleFontIta;
            paras[21].parName = "@TitleFontIta";

            paras[22].dbType = SqlDbType.Int;
            paras[22].objValue = model.MaxCharPerPage;
            paras[22].parName = "@MaxCharPerPage";

            paras[23].dbType = SqlDbType.Int;
            paras[23].objValue = model.AppraiseNum;
            paras[23].parName = "@AppraiseNum";

            paras[24].dbType = SqlDbType.Int;
            paras[24].objValue = model.ChannelTypeID;
            paras[24].parName = "@ChannelTypeID";

            paras[25].dbType = SqlDbType.Int;
            paras[25].objValue = model.ChannelType;
            paras[25].parName = "@ChannelType";

            string cmdText = "INSERT INTO [Article] VALUES (@Title,@Author,@CopyFrom,@IsMaster,@EditorId,@KeyWord,@Hits,@UpdateTime,@IsHot,@IsTop,@IsCommend,@IsPassed,@Content,@IncludePic,@DefaultPicUrl,@ReadScore,@ClassID,@IsDel,@TitleFontColor,@TitleFontType,@TitleFontBlod,@TitleFontIta,@MaxCharPerPage,@AppraiseNum,@ChannelTypeID,@ChannelType)";
            int count = 0;
            try
            { 
                count=dbOperate.ExcSql(cmdText, paras);
            }
            catch(Exception ex)
            {
                
            }
            return count;
        }

        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateArticle(Article_Model model)
        {
            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[27];
            
            //设置参数的值
            paras[0].dbType = SqlDbType.Text;
            paras[0].objValue = model.Title;
            paras[0].parName = "@Title";

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].objValue = model.Author;
            paras[1].parName = "@Author";

            paras[2].dbType = SqlDbType.VarChar;
            paras[2].objValue = model.CopyFrom;
            paras[2].parName = "@CopyFrom";

            paras[3].dbType = SqlDbType.Int;
            paras[3].objValue = model.IsMaster;
            paras[3].parName = "@IsMaster";

            paras[4].dbType = SqlDbType.Int;
            paras[4].objValue = model.EditorId;
            paras[4].parName = "@EditorId";

            paras[5].dbType = SqlDbType.VarChar;
            paras[5].objValue = model.KeyWord;
            paras[5].parName = "@KeyWord";

            paras[6].dbType = SqlDbType.Int;
            paras[6].objValue = model.Hits;
            paras[6].parName = "@Hits";


            paras[7].dbType = SqlDbType.DateTime;
            paras[7].objValue = model.UpdateTime;
            paras[7].parName = "@UpdateTime";

            paras[8].dbType = SqlDbType.Int;
            paras[8].objValue = model.IsHot;
            paras[8].parName = "@IsHot";

            paras[9].dbType = SqlDbType.Int;
            paras[9].objValue = model.IsTop;
            paras[9].parName = "@IsTop";

            paras[10].dbType = SqlDbType.Int;
            paras[10].objValue = model.IsCommend;
            paras[10].parName = "@IsCommend";

            paras[11].dbType = SqlDbType.Int;
            paras[11].objValue = model.IsPassed;
            paras[11].parName = "@IsPassed";

            paras[12].dbType = SqlDbType.Text;
            paras[12].objValue = model.Content;
            paras[12].parName = "@Content";

            paras[13].dbType = SqlDbType.Int;
            paras[13].objValue = model.IncludePic;
            paras[13].parName = "@IncludePic";

            paras[14].dbType = SqlDbType.VarChar;
            paras[14].objValue = model.DefaultPicUrl;
            paras[14].parName = "@DefaultPicUrl";

            paras[15].dbType = SqlDbType.Int;
            paras[15].objValue = model.ReadScore;
            paras[15].parName = "@ReadScore";

            paras[16].dbType = SqlDbType.Int;
            paras[16].objValue = model.ClassID;
            paras[16].parName = "@ClassID";

            paras[17].dbType = SqlDbType.Int;
            paras[17].objValue = model.IsDel;
            paras[17].parName = "@IsDel";

            paras[18].dbType = SqlDbType.VarChar;
            paras[18].objValue = model.TitleFontColor;
            paras[18].parName = "@TitleFontColor";

            paras[19].dbType = SqlDbType.Int;
            paras[19].objValue = model.TitleFontType;
            paras[19].parName = "@TitleFontType";

            paras[20].dbType = SqlDbType.Int;
            paras[20].objValue = model.TitleFontBlod;
            paras[20].parName = "@TitleFontBlod";

            paras[21].dbType = SqlDbType.Int;
            paras[21].objValue = model.TitleFontIta;
            paras[21].parName = "@TitleFontIta";

            paras[22].dbType = SqlDbType.Int;
            paras[22].objValue = model.MaxCharPerPage;
            paras[22].parName = "@MaxCharPerPage";

            paras[23].dbType = SqlDbType.Int;
            paras[23].objValue = model.AppraiseNum;
            paras[23].parName = "@AppraiseNum";

            paras[24].dbType = SqlDbType.Int;
            paras[24].objValue = model.ChannelTypeID;
            paras[24].parName = "@ChannelTypeID";

            paras[25].dbType = SqlDbType.Int;
            paras[25].objValue = model.ChannelType;
            paras[25].parName = "@ChannelType";

            paras[26].dbType = SqlDbType.Int;
            paras[26].objValue = model.ArticleID;
            paras[26].parName = "@ArticleID";

            string cmdText = "update  Article set Title=@Title,Author=@Author,CopyFrom=@CopyFrom,IsMaster=@IsMaster,EditorId=@EditorId,KeyWord=@KeyWord,Hits=@Hits,UpdateTime=@UpdateTime,IsHot=@IsHot,IsTop=@IsTop,IsCommend=@IsCommend,IsPassed=@IsPassed,Content=@Content,IncludePic=@IncludePic,DefaultPicUrl=@DefaultPicUrl,ReadScore=@ReadScore,ClassID=@ClassID,IsDel=@IsDel,TitleFontColor=@TitleFontColor,TitleFontType=@TitleFontType,TitleFontBlod=@TitleFontBlod,TitleFontIta=@TitleFontIta,MaxCharPerPage=@MaxCharPerPage,AppraiseNum=@AppraiseNum,ChannelTypeID=@ChannelTypeID,ChannelType=@ChannelType where ArticleID=@ArticleID";
            int count = 0;
            try
            { 
                count=dbOperate.ExcSql(cmdText, paras);
            }
            catch(Exception ex)
            {
                
            }
            return count;
        
        }

        //通过文章编号获得文章详细
        public Article_Model getArticleModelByArticleID(int ArticleID)
        {
            Article_Model model = new Article_Model();
            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[1];
            paras[0].dbType = SqlDbType.Int;
            paras[0].objValue = ArticleID;
            paras[0].parName = "@ArticleID";
            string cmdText = "SELECT * FROM [Article] where ArticleID=@ArticleID";
            SqlDataReader reader = dbOperate.GetSqlDataReader(cmdText,paras);
            while(reader.Read())
            {
                model.ArticleID = reader.GetInt32(0);
                model.Title = reader.GetString(1);
                model.Author = reader.GetString(2);
                model.CopyFrom = reader.GetString(3);
                model.IsMaster = reader.GetInt32(4);
                model.EditorId = reader.GetInt32(5);
                model.KeyWord = reader.GetString(6);
                model.Hits = reader.GetInt32(7);
                model.UpdateTime = reader.GetDateTime(8);
                model.IsHot = reader.GetInt32(9);
                model.IsTop = reader.GetInt32(10);
                model.IsCommend = reader.GetInt32(11);
                model.IsPassed = reader.GetInt32(12);
                model.Content = reader.GetString(13);
                model.IncludePic = reader.GetInt32(14);
                model.DefaultPicUrl = reader.GetString(15);
                model.ReadScore = reader.GetInt32(16);
                model.ClassID = reader.GetInt32(17);
                model.IsDel = reader.GetInt32(18);
                model.TitleFontColor = reader.GetString(19);
                model.TitleFontType = reader.GetInt32(20);
                model.TitleFontBlod = reader.GetInt32(21);
                model.TitleFontIta = reader.GetInt32(22);
                model.MaxCharPerPage = reader.GetInt32(23);
                model.AppraiseNum = reader.GetInt32(24);
                model.ChannelTypeID = reader.GetInt32(25);
                model.ChannelType = reader.GetInt32(26);
            }
            dbOperate.close();
            dbOperate.Dispose();
            return model;
        }

        /// <summary>
        /// 删除文章(逻辑删除)
        /// </summary>
        /// <param name="ArticleID"></param>
        /// <returns></returns>
        public int DeleteArticle(int ArticleID)
        {
            string cmdText = "update Article set IsDel=1 where ArticleID=@ArticleID";
            
            db dbOperate = new db();
            db.ParameterArry[] paras=new db.ParameterArry[1];
            paras[0].dbType=SqlDbType.Int;
            paras[0].objValue=ArticleID;
            paras[0].parName="@ArticleID";

            int count = 0;
            count = dbOperate.ExcSql(cmdText, paras);
            return count;
        }

        /// <summary>
        /// 删除文章(物理删除)
        /// </summary>
        /// <param name="ArticleID"></param>
        /// <returns></returns>
        public int PhyicDeleteArticle(int ArticleID)
        {
            string cmdText = "delete from Article where ArticleID=@ArticleID";

            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[1];
            paras[0].dbType = SqlDbType.Int;
            paras[0].objValue = ArticleID;
            paras[0].parName = "@ArticleID";

            int count = 0;
            count = dbOperate.ExcSql(cmdText, paras);
            return count;
        }


        /// <summary>
        /// 审核通过
        /// </summary>
        /// <param name="ArticleID"></param>
        /// <returns></returns>
        public int CheckArticle(int ArticleID)
        {
            string cmdText = "update Article set IsPassed=1 where ArticleID=@ArticleID";

            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[1];
            paras[0].dbType = SqlDbType.Int;
            paras[0].objValue = ArticleID;
            paras[0].parName = "@ArticleID";

            int count = 0;
            count = dbOperate.ExcSql(cmdText, paras);
            return count;
        }

        /// <summary>
        /// 恢复被删除的文章
        /// </summary>
        /// <param name="ArticleID"></param>
        /// <returns></returns>
        public int RebackArticle(int ArticleID)
        {
            string cmdText = "update Article set IsDel=0 where ArticleID=@ArticleID";

            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[1];
            paras[0].dbType = SqlDbType.Int;
            paras[0].objValue = ArticleID;
            paras[0].parName = "@ArticleID";

            int count = 0;
            count = dbOperate.ExcSql(cmdText, paras);
            return count;
        }

        /// <summary>
        /// 恢复所有被删除的文章
        /// </summary>
        /// <returns></returns>
        public int RebackAllArticle()
        {
            string cmdText = "update Article set IsDel=0 where IsDel=1";
            db dbOperate = new db();
            int count = 0;
            count = dbOperate.ExcSql(cmdText);
            return count;
        }

        /// <summary>
        /// 获得各种不同类型的文章
        /// </summary>
        /// <param name="queryPropertys">要查询的字段(Example:"id,name,user_name"),查询所有字段用"*"</param>
        /// <param name="IsMaster">是否是管理员</param>
        /// <param name="EditorId">发布者编号</param>
        /// <param name="ChannelTypeID">频道类型id(如公墓id、纪念馆id)</param>
        /// <param name="channelType">文章类型用枚举值Channel_Type表示(如公墓、纪念馆)</param>
        /// <returns></returns>
        public DataTable getKindsOfArticle(string queryPropertys, int IsMaster, int EditorId, int ChannelTypeID, Channel_Type channelType, int ClassId)
        {
            DataTable dataTable = new DataTable();
            string cmdText = "select "+queryPropertys+" from Article where IsMaster=@IsMaster and EditorId=@EditorId and ChannelTypeID=@ChannelTypeID and ChannelType=@ChannelType and ClassID=@ClassID and IsDel=0";

            db.ParameterArry[] paras = new db.ParameterArry[5];

            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@IsMaster";
            paras[0].objValue = IsMaster;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@EditorId";
            paras[1].objValue = EditorId;

            paras[2].dbType = SqlDbType.Int;
            paras[2].parName = "@ChannelTypeID";
            paras[2].objValue = ChannelTypeID;

            paras[3].dbType = SqlDbType.Int;
            paras[3].parName = "@ChannelType";
            paras[3].objValue = (int)channelType;


            paras[4].dbType = SqlDbType.Int;
            paras[4].parName = "@ClassID";
            paras[4].objValue = ClassId;

            db dbclass = new db();
            dataTable = dbclass.GetSql_DataTable(cmdText,paras);

            return dataTable;
        }

        /// <summary>
        /// 设置或者取消置顶
        /// </summary>
        /// <param name="ArticleId">文章编号</param>
        /// <param name="SubmitOrCancle">0:取消置顶 1:设置置顶</param>
        /// <returns></returns>
        public int SetTop(int ArticleId, int SubmitOrCancle)
        {
            string cmdText = "update Article set IsTop=@IsTop where ArticleID=@ArticleID";

            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].objValue = ArticleId;
            paras[0].parName = "@ArticleID";

            paras[1].dbType = SqlDbType.Int;
            paras[1].objValue = SubmitOrCancle;
            paras[1].parName = "@IsTop";

            int count = 0;
            count = dbOperate.ExcSql(cmdText, paras);
            return count;
        }

        /// <summary>
        /// 设置或者取消推荐
        /// </summary>
        /// <param name="ArticleId">文章编号</param>
        /// <param name="SubmitOrCancle">0:取消推荐 1:设置推荐</param>
        /// <returns></returns>
        public int SetCommend(int ArticleId, int SubmitOrCancle)
        {
            string cmdText = "update Article set IsCommend=@IsCommend where ArticleID=@ArticleID";

            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].objValue = ArticleId;
            paras[0].parName = "@ArticleID";

            paras[1].dbType = SqlDbType.Int;
            paras[1].objValue = SubmitOrCancle;
            paras[1].parName = "@IsCommend";

            int count = 0;
            count = dbOperate.ExcSql(cmdText, paras);
            return count;
        }


        /// <summary>
        /// 设置或者取消热点
        /// </summary>
        /// <param name="ArticleId">文章编号</param>
        /// <param name="SubmitOrCancle">0:取消热点 1:设置热点</param>
        /// <returns></returns>
        public int SetHot(int ArticleId, int SubmitOrCancle)
        {
            string cmdText = "update Article set IsHot=@IsHot where ArticleID=@ArticleID";

            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].objValue = ArticleId;
            paras[0].parName = "@ArticleID";

            paras[1].dbType = SqlDbType.Int;
            paras[1].objValue = SubmitOrCancle;
            paras[1].parName = "@IsHot";

            int count = 0;
            count = dbOperate.ExcSql(cmdText, paras);
            return count;
        }

        /// <summary>
        /// 设置或者取消审核
        /// </summary>
        /// <param name="ArticleId">文章编号</param>
        /// <param name="SubmitOrCancle">0:取消审核 1:设置审核</param>
        /// <returns></returns>
        public int SetCheck(int ArticleId, int SubmitOrCancle)
        {
            string cmdText = "update Article set IsPassed=@IsPassed where ArticleID=@ArticleID";

            db dbOperate = new db();
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].objValue = ArticleId;
            paras[0].parName = "@ArticleID";

            paras[1].dbType = SqlDbType.Int;
            paras[1].objValue = SubmitOrCancle;
            paras[1].parName = "@IsPassed";

            int count = 0;
            count = dbOperate.ExcSql(cmdText, paras);
            return count;
        }


    }
}
